package com.srmt.common.exception;

/**
 * 429速率限制异常
 * 当API调用超过速率限制时抛出
 * 
 * @author SRMT Development Team
 * @since 1.0.0
 */
public class RateLimitExceededException extends RuntimeException {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * HTTP状态码
     */
    private final int httpStatusCode;
    
    /**
     * API端点
     */
    private final String apiEndpoint;
    
    /**
     * 建议的重试延迟（毫秒）
     */
    private final long suggestedRetryDelayMs;
    
    /**
     * 构造函数
     * 
     * @param message 错误消息
     * @param httpStatusCode HTTP状态码
     * @param apiEndpoint API端点
     * @param suggestedRetryDelayMs 建议的重试延迟
     */
    public RateLimitExceededException(String message, int httpStatusCode, 
                                     String apiEndpoint, long suggestedRetryDelayMs) {
        super(message);
        this.httpStatusCode = httpStatusCode;
        this.apiEndpoint = apiEndpoint;
        this.suggestedRetryDelayMs = suggestedRetryDelayMs;
    }
    
    /**
     * 构造函数（带原因）
     * 
     * @param message 错误消息
     * @param cause 原因
     * @param httpStatusCode HTTP状态码
     * @param apiEndpoint API端点
     * @param suggestedRetryDelayMs 建议的重试延迟
     */
    public RateLimitExceededException(String message, Throwable cause, int httpStatusCode,
                                     String apiEndpoint, long suggestedRetryDelayMs) {
        super(message, cause);
        this.httpStatusCode = httpStatusCode;
        this.apiEndpoint = apiEndpoint;
        this.suggestedRetryDelayMs = suggestedRetryDelayMs;
    }
    
    /**
     * 简化构造函数
     * 
     * @param apiEndpoint API端点
     */
    public RateLimitExceededException(String apiEndpoint) {
        this(String.format("Rate limit exceeded for API: %s (HTTP 429)", apiEndpoint),
             429, apiEndpoint, 1000);
    }
    
    /**
     * 获取HTTP状态码
     * 
     * @return HTTP状态码
     */
    public int getHttpStatusCode() {
        return httpStatusCode;
    }
    
    /**
     * 获取API端点
     * 
     * @return API端点
     */
    public String getApiEndpoint() {
        return apiEndpoint;
    }
    
    /**
     * 获取建议的重试延迟
     * 
     * @return 建议的重试延迟（毫秒）
     */
    public long getSuggestedRetryDelayMs() {
        return suggestedRetryDelayMs;
    }
    
    @Override
    public String toString() {
        return String.format("RateLimitExceededException{httpStatusCode=%d, apiEndpoint='%s', " +
                           "suggestedRetryDelayMs=%d, message='%s'}",
                           httpStatusCode, apiEndpoint, suggestedRetryDelayMs, getMessage());
    }
}